import { isNull } from "/handlers/isNull.js";
const indexHandler = {
    loginHandler: (a, p) => {
        return new Promise((resolve, reject) => {
            if (!isNull(a) && !isNull(p)) {
                axios.post('/users/login', {
                    "account": a,
                    "password": p
                }).then(res => {
                    console.log('登录结果', res.data);
                    if (res.data.ok === 1) {
                        resolve(1);
                    } else {
                        resolve(2);
                    }
                }).catch(err => {
                    resolve(3);
                })
            } else if (isNull(a)) {
                resolve(4);
            } else resolve(5);
        })
    },
    registerHandler: (a, nick, p1, p2) => {
        return new Promise((resolve, reject) => {
            if (!isNull(a) && !isNull(nick) && !isNull(p1) && !isNull(p2)) {
                if (a.length > 9 || nick.length > 9) {
                    resolve(4);
                } else {
                    if (p1 === p2) {
                        axios.post('/users/register', {
                            "account": a,
                            "password": p1,
                            "nickname": nick
                        }).then(res => {
                            console.log('注册结果', res.data);
                            if (res.data.ok === 1) {
                                resolve(1);
                            } else if (res.data.ok === 2) {
                                resolve(2);
                            } else {
                                resolve(0);
                            }
                        }).catch(err => {
                            resolve(3);
                        })
                    } else {
                        resolve(5);  
                    }
                }
            } else {
                resolve(6);
            }
        })
    }
}
export default indexHandler